<?php



//微信检测敏感内容
function text($appid,$content)
{
    if(empty($appid) || empty($content)){
        return false;
    }
    $url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.getnewAccess_token($appid);
   
    $data_content['content'] = $content;
    $data_content = json_encode($data_content);
    
   $res =  sendCmd($url,$data_content);
   $data = json_decode($res,true);
   if(isset($data['errcode']) &&$data['errcode'] == 87014 ){
        return ['code'=>404,'msg'=>'内容含有违法违规内容'];
        // echo $data['errmsg'];
   }elseif(isset($data['errcode']) &&$data['errcode'] <= 0){
        return ['code'=>0,'msg'=>'内容正常'];
        // echo $data['errmsg'];
   }else{
        return ['code'=>500,'msg'=>'api请求错误'];
   }
}
//检测图片
 function img($appid,$img)
{
    if(empty($appid) || empty($img)){
        return false;
    }

    $ext = explode(".", $img);
    $ext = $ext[count($ext)-1];
    
    $img = file_get_contents($img);
    $filePath = ROOT_PATH.'public/uploads/wxcode/tmp1.'.$ext;
    file_put_contents($filePath, $img);

    resize_image($filePath, $filePath);//把尺寸缩放到规定大小(暂时把图片放在后端，小程序在审核中没办法改)

    //拼接文件发送格式
    $minetype = 'image/jpeg';
    $curl_file = curl_file_create($filePath,$minetype);


    $url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token='.getNewWxToken($appid);
   
    $data_content['media'] = $curl_file;
    
    $res =  sendCmd($url,$data_content);
    $data = json_decode($res,true);
  
    if(isset($data['errcode']) &&$data['errcode'] == 87014 ){
        return ['code'=>404,'msg'=>'图片中含有敏感信息,禁止上传'];
   }elseif(isset($data['errcode']) &&$data['errcode'] <= 0){
        return ['code'=>0,'msg'=>'内容正常'];
   }else{
        return ['code'=>500,'msg'=>'api请求错误'];
   }
}
//设置图片大小
function resize_image($filename, $tmpname, $xmax=750, $ymax=1334)
{
    $ext = explode(".", $filename);
    $ext = $ext[count($ext)-1];

    if($ext == "jpg" || $ext == "jpeg")
        $im = imagecreatefromjpeg($tmpname);
    elseif($ext == "png")
        $im = imagecreatefrompng($tmpname);
    elseif($ext == "gif")
        $im = imagecreatefromgif($tmpname);

    $x = imagesx($im);
    $y = imagesy($im);

    if($x <= $xmax && $y <= $ymax)
        return $im;

    if($x >= $y) {
        $newx = $xmax;
        $newy = $newx * $y / $x;
    }
    else {
        $newy = $ymax;
        $newx = $x / $y * $newy;
    }

    $im2 = imagecreatetruecolor($newx, $newy);
    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
    return $im2;
}

//开启curl post请求
function sendCmd($url,$data)
{
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回

    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
        echo 'Errno'.curl_error($curl);
    }
    curl_close($curl); // 关键CURL会话
    return $tmpInfo; // 返回数据
}


function httpCurl($url, $params, $method = 'GET', $header = array(), $multi = false){
    
    date_default_timezone_set('PRC');
    $opts = array(
        CURLOPT_TIMEOUT        => 30,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_HTTPHEADER     => $header,
        CURLOPT_COOKIESESSION  => true,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_COOKIE =>session_name().'='.session_id(),
    );

    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            // $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            // 链接后拼接参数  &  非？
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':                //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }

    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return  $data;

}

?>